package com.example.medicalapp.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.example.medicalapp.R;
import com.example.medicalapp.util.PreferencesUtil;

public class WelcomeActivity extends AppCompatActivity {
    private static final String TAG = "WelcomeActivity";
    private static final long DELAY_MILLIS = 500; // 0.5秒延迟
    private PreferencesUtil preferencesUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        Log.d(TAG, "onCreate called");

        preferencesUtil = new PreferencesUtil(this);

        // 延迟后跳转到对应主页
        new Handler(Looper.getMainLooper()).postDelayed(this::navigateToHome, DELAY_MILLIS);
    }

    private void navigateToHome() {
        // 获取用户角色
        String role = preferencesUtil.getRole();
        Log.d(TAG, "Navigating to home for role: " + role);

        Intent intent;
        if ("doctor".equals(role)) {
            // 医生角色跳转到医生主页
            intent = new Intent(WelcomeActivity.this, DoctorHomeActivity.class);
        } else {
            // 患者角色跳转到患者主页
            intent = new Intent(WelcomeActivity.this, PatientHomeActivity.class);
        }

        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        finish(); // 结束欢迎页面，防止返回
    }
}